package com.itheima.consumer.config;


import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Author:Cammy.Wu
 * Date:2024-05-27
 * Description:
 */

@Configuration
public class DirectConfig {

    /**
     * 声明交换机
     * @return Direct类型交换机
     */
//    @Bean
//    public DirectExchange directExchange(){
//        return ExchangeBuilder.directExchange("hmall.direct1").build();
//    }
//
//    /**
//     * 第1个队列
//     */
//    @Bean
//    public Queue directQueue1(){
//        return new Queue("direct.queue3");
//    }
//
//    /**
//     * 绑定队列和交换机
//     */
//    @Bean
//    public Binding bindingQueue1WithRed(Queue directQueue1, DirectExchange directExchange){
//        return BindingBuilder.bind(directQueue1).to(directExchange).with("red");
//    }
//    /**
//     * 绑定队列和交换机
//     */
//    @Bean
//    public Binding bindingQueue1WithBlue(Queue directQueue1, DirectExchange directExchange){
//        return BindingBuilder.bind(directQueue1).to(directExchange).with("blue");
//    }
//
//    // ---------------------------------------------------------------------------------------
//
//    /**
//     * 第2个队列
//     */
//    @Bean
//    public Queue directQueue2(){
//        return new Queue("direct.queue4");
//    }
//
//    /**
//     * 绑定队列和交换机
//     */
//    @Bean
//    public Binding bindingQueue2WithRed(Queue directQueue2, DirectExchange directExchange){
//        return BindingBuilder.bind(directQueue2).to(directExchange).with("red");
//    }
//    /**
//     * 绑定队列和交换机
//     */
//    @Bean
//    public Binding bindingQueue2WithYellow(Queue directQueue2, DirectExchange directExchange){
//        return BindingBuilder.bind(directQueue2).to(directExchange).with("yellow");
//    }

    // ---------------------------------------------------------------------------------------


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue5"),
            exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}
    ))
    public void listenDirectQueue1(String msg){
        System.out.println("消费者1接收到direct.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue6"),
            exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}
    ))
    public void listenDirectQueue2(String msg){
        System.out.println("消费者2接收到direct.queue2的消息：【" + msg + "】");
    }
}
